package com.omesoft.hksnore.util.json;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.omesoft.hksnore.R;
import com.omesoft.hksnore.util.constant.CusConstant;
import com.omesoft.hksnore.util.data.MySharedPreferencesUtil;
import com.omesoft.hksnore.util.dialog.MyProgressBarDialogUtil;
import com.omesoft.hksnore.util.thread.MyThread;
import com.omesoft.hksnore.util.web.WebServiceUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by BigFace on 2017/3/21.
 */
public class HypnotistWSUtil {
    /**
     * 获取催眠大师睡眠报告
     *
     * @param context
     * @param handler
     */
    public static void GetHypnotistSleepReportForShare(final Context context, final Handler handler, final String sleepID, final int type) {
        try {
            try {
                if (type == 6) // 复制用
                    MyProgressBarDialogUtil.show(context, R.string.share_copying);
                else
                    MyProgressBarDialogUtil.show(context, R.string.sharing);
            } catch (Exception e) {
                e.printStackTrace();
            }
            MyThread.startNewThread(new Runnable() {

                @Override
                public void run() {
                    HashMap<String, Object> param = new HashMap<String, Object>();
                    param.put("app_id", CusConstant.APPID);
                    param.put("member_id", MySharedPreferencesUtil.getMemberId(context));
                    param.put("client_key", MySharedPreferencesUtil.getCilenKey(context));
                    param.put("sleep_id", sleepID);

                    JSONObject object = new JSONObject(param);
                    String infoJson = object.toString();
                    HashMap<String, String> PARAMS = new HashMap<String, String>();
                    PARAMS.put("dataJson", infoJson);
                    Log.v("HypnotistWSUtil", "GetHypnotistSleepReportForShare::PARAMS::" + PARAMS);
                    String resultStr = WebServiceUtils.callDotNetWS("GetSleepReportForShare", PARAMS);
                    Log.v("HypnotistWSUtil", "GetHypnotistSleepReportForShare::resultStr::" + resultStr);
                    if (resultStr != null) {
                        try {
                            JSONObject temp = new JSONObject(resultStr);
                            int err_code = temp.getInt("err_code");
                            String msg = temp.getString("msg");
                            Message message = Message.obtain();
                            if (handler != null) {
                                if (err_code == 0) {
                                    JSONObject data = temp.getJSONObject("data");
                                    String url = data.getString("url");
                                    message.what = 0;
                                    message.obj = url;
                                    message.arg1 = type;
                                    handler.sendMessage(message);
                                } else {
                                    message.what = err_code;
                                    message.obj = msg;
                                    message.arg1 = type;
                                    handler.sendMessage(message);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            if (handler != null) {
                                handler.sendEmptyMessage(ReErrorCode.ERRCODE_UNKNOWN_EXCEPTION);
                            }
                        }
                    } else {
                        handler.sendEmptyMessage(ReErrorCode.ERRCODE_NETWORK_ANOMALY);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            Log.v("HypnotistWSUtil", "error");
        }
    }

    /**
     * 获取催眠大师睡眠详细数据
     *
     * @param context
     * @param handler
     */
    public static void GetSleepDetailBySleepId(final Context context, final Handler handler, final String sleepID) {
        try {
            // MyProgressBarDialogUtil.show(context, R.string.sharing);
            MyThread.startNewThread(new Runnable() {

                @Override
                public void run() {
                    HashMap<String, Object> param = new HashMap<String, Object>();
                    param.put("app_id", CusConstant.APPID);
                    param.put("member_id", MySharedPreferencesUtil.getMemberId(context));
                    param.put("client_key", MySharedPreferencesUtil.getCilenKey(context));
                    param.put("sleep_id", sleepID);

                    JSONObject object = new JSONObject(param);
                    String infoJson = object.toString();
                    HashMap<String, String> PARAMS = new HashMap<String, String>();
                    PARAMS.put("dataJson", infoJson);
//                    Log.v("HypnotistWSUtil", "PARAMS" + PARAMS.toString());

                    String resultStr = WebServiceUtils.callDotNetWS("GetSleepDetailBySleepId", PARAMS);
//                    Log.v("HypnotistWSUtil", "GetSleepDetailBySleepId::resultStr::" + resultStr);
                    if (resultStr != null) {
                        try {
                            JSONObject temp = new JSONObject(resultStr);
                            int err_code = temp.getInt("err_code");
                            String msg = temp.getString("msg");
                            Message message = Message.obtain();
                            if (err_code == 0) {
                                if (err_code == 0) {
                                    JSONObject data = temp.getJSONObject("data");
                                    JSONArray snoreData = data.getJSONArray("sleep_snore");
                                    // 插入睡眠详细数据
                                    SyncUtil.setSnoreData(context, snoreData);
                                    message.what = CusConstant.SCYN_SLEEP_DETAIL_COMPLETE;
                                    handler.sendMessage(message);
                                } else {
                                    message.what = CusConstant.SCYN_SLEEP_DETAIL_COMPLETE_FAILED;
                                    message.obj = msg;
                                    handler.sendMessage(message);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            if (handler != null) {

                            }
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            Log.v("HypnotistWSUtil", "error");
        }
    }

    /**
     * 任务-分享催眠大师混音
     *
     * @param context
     * @param handler
     */
    public static void TaskShareHypnotistMixAudio(final Context context, final Handler handler) {
        try {
            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    HashMap<String, Object> param = new HashMap<String, Object>();
                    param.put("app_id", CusConstant.APPID);
                    param.put("member_id", MySharedPreferencesUtil.getMemberId(context));
                    param.put("client_key", MySharedPreferencesUtil.getCilenKey(context));

                    JSONObject object = new JSONObject(param);
                    String infoJson = object.toString();
                    HashMap<String, String> PARAMS = new HashMap<String, String>();
                    PARAMS.put("dataJson", infoJson);
                    Log.v("HypnotistWSUtil", "TaskShareHypnotistMixAudio::PARAMS::" + PARAMS);
                    String resultStr = WebServiceUtils.callDotNetWS("TaskForShareMixAudio", PARAMS);
                    Log.v("HypnotistWSUtil", "TaskShareHypnotistMixAudio::resultStr::" + resultStr);
                    if (resultStr != null) {
                        try {
                            JSONObject temp = new JSONObject(resultStr);
                            int err_code = temp.getInt("err_code");
                            String msg = temp.getString("msg");
                            Message message = Message.obtain();
                            if (handler != null) {
                                if (err_code == 0) {
                                    JSONObject jsonObj = temp.getJSONObject("data");
                                    message.what = err_code;
                                    message.obj = jsonObj;
                                    handler.sendMessage(message);
                                } else {
                                    message.what = err_code;
                                    message.obj = msg;
                                    handler.sendMessage(message);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            if (handler != null) {

                            }
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            Log.v("AccsessMXTaskWSUtil", "error");
        }
    }

    /**
     * 任务-每日签到
     *
     * @param context
     * @param handler
     */
    public static void TaskSignIn(final Context context, final Handler handler) {
        try {
            MyThread.startNewThread(new Runnable() {

                @Override
                public void run() {
                    HashMap<String, Object> param = new HashMap<String, Object>();
                    param.put("app_id", CusConstant.APPID);
                    param.put("member_id", MySharedPreferencesUtil.getMemberId(context));
                    param.put("client_key", MySharedPreferencesUtil.getCilenKey(context));

                    JSONObject object = new JSONObject(param);
                    String infoJson = object.toString();
                    HashMap<String, String> PARAMS = new HashMap<String, String>();
                    PARAMS.put("dataJson", infoJson);
                    Log.v("HypnotistWSUtil", "TaskSignIn::PARAMS::" + PARAMS);
                    String resultStr = WebServiceUtils.callDotNetWS("TaskForSignIn", PARAMS);
                    Log.v("HypnotistWSUtil", "TaskSignIn::resultStr::" + resultStr);
                    if (resultStr != null) {
                        try {
                            JSONObject temp = new JSONObject(resultStr);
                            int err_code = temp.getInt("err_code");
                            String msg = temp.getString("msg");
                            Message message = Message.obtain();
                            if (handler != null) {
                                if (err_code == 0) {
                                    JSONObject jsonObj = temp.getJSONObject("data");
                                    Log.v("HypnotistWSUtil", "TaskSignIn::jsonObj::" + jsonObj.toString());
                                    message.what = err_code;
                                    message.obj = jsonObj;
                                    handler.sendMessage(message);
                                } else {
                                    // message.what = err_code;
                                    // message.obj = msg;
                                    // handler.sendMessage(message);
                                }

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            if (handler != null) {

                            }
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            Log.v("HypnotistWSUtil", "error");
        }
    }

    /**
     * 任务-点赞
     *
     * @param context
     * @param handler
     */
    public static void taskLikeThread(final Context context, final Handler handler, final int thread_id) {
        try {
            MyThread.startNewThread(new Runnable() {

                @Override
                public void run() {
                    HashMap<String, Object> param = new HashMap<String, Object>();
                    param.put("app_id", CusConstant.APPID);
                    param.put("member_id", MySharedPreferencesUtil.getMemberId(context));
                    param.put("client_key", MySharedPreferencesUtil.getCilenKey(context));
                    param.put("thread_id", thread_id);

                    JSONObject object = new JSONObject(param);
                    String infoJson = object.toString();
                    HashMap<String, String> PARAMS = new HashMap<String, String>();
                    PARAMS.put("dataJson", infoJson);
                    Log.v("HypnotistWSUtil", "TaskLikeThread::PARAMS::" + PARAMS);
                    String resultStr = WebServiceUtils.callDotNetWS("TaskLikeThread", PARAMS);
                    if (resultStr != null) {
                        Log.v("HypnotistWSUtil", "TaskLikeThread::resultStr::" + resultStr);
                        try {
                            JSONObject temp = new JSONObject(resultStr);
                            int err_code = temp.getInt("err_code");
                            String msg = temp.getString("msg");
                            Message message = Message.obtain();
                            if (handler != null) {
                                if (err_code == 0) {
                                    JSONObject jsonObj = temp.getJSONObject("data");
                                    Log.v("HypnotistWSUtil", "TaskLikeThread::jsonObj::" + jsonObj.toString());
                                    message.what = ReErrorCode.SUCEED_TaskLikeThread;
                                    message.obj = jsonObj;
                                    handler.sendMessage(message);
                                } else {
                                    message.what = err_code;
                                    message.obj = msg;
                                    message.arg1 = ReErrorCode.SUCEED_TaskLikeThread;
                                    handler.sendMessage(message);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 任务-评论
     *
     * @param context
     * @param handler
     */
    public static void taskReviewThread(final Context context, final Handler handler, final int thread_id) {
        try {
            MyThread.startNewThread(new Runnable() {

                @Override
                public void run() {
                    HashMap<String, Object> param = new HashMap<String, Object>();
                    param.put("app_id", CusConstant.APPID);
                    param.put("member_id", MySharedPreferencesUtil.getMemberId(context));
                    param.put("client_key", MySharedPreferencesUtil.getCilenKey(context));
                    param.put("thread_id", thread_id);

                    JSONObject object = new JSONObject(param);
                    String infoJson = object.toString();
                    HashMap<String, String> PARAMS = new HashMap<String, String>();
                    PARAMS.put("dataJson", infoJson);
                    Log.v("HypnotistWSUtil", "TaskReviewThread::PARAMS::" + PARAMS);
                    String resultStr = WebServiceUtils.callDotNetWS("TaskReviewThread", PARAMS);
                    if (resultStr != null) {
                        Log.v("HypnotistWSUtil", "TaskReviewThread::resultStr::" + resultStr);
                        try {
                            JSONObject temp = new JSONObject(resultStr);
                            int err_code = temp.getInt("err_code");
                            String msg = temp.getString("msg");
                            Message message = Message.obtain();
                            if (handler != null) {
                                if (err_code == 0) {
                                    JSONObject jsonObj = temp.getJSONObject("data");
                                    Log.v("HypnotistWSUtil", "TaskReviewThread::jsonObj::" + jsonObj.toString());
                                    message.what = ReErrorCode.SUCEED_TaskReviewThread;
                                    message.obj = jsonObj;
                                    handler.sendMessage(message);
                                } else {
                                    message.what = err_code;
                                    message.obj = msg;
                                    handler.sendMessage(message);
                                }

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 激活接口
     *
     * @param context
     * @param handler
     * @param cardNumber
     * @param pinNumber
     */
//    public static void Activate(final Context context, final Handler handler, final String cardNumber, final String pinNumber,
//                                final ActivateConstant activateConstant) {
//        try {
//            MyThread.startNewThread(new Runnable() {
//                @Override
//                public void run() {
//                    // TODO Auto-generated method stub
//                    HashMap<String, Object> param = new HashMap<String, Object>();
//                    param.put("app_id", CusConstant.APPID);// 催眠大师104
//                    param.put("member_id", MySharedPreferencesUtil.getMemberId(context));
//                    param.put("client_key", MySharedPreferencesUtil.getCilenKey(context));
//                    param.put("card_number", cardNumber);
//                    param.put("pin_number", pinNumber);
//                    param.put("type", activateConstant.toString());
//                    JSONObject object = new JSONObject(param);
//                    String infoJson = object.toString();
//                    HashMap<String, String> PARAMS = new HashMap<String, String>();
//                    PARAMS.put("dataJson", infoJson);
//                    String resultStr = WebServiceUtils.callDotNetWS("Activate", PARAMS);
//                    Log.v("activate", "resultStr::" + resultStr);
//                    if (resultStr != null) {
//                        try {
//                            JSONObject temp = new JSONObject(resultStr);
//                            int err_code = temp.getInt("err_code");
//                            String msg = temp.getString("msg");
//                            if (err_code == 0) {
//                                if (activateConstant.equals(ActivateConstant.BONE))// 骨传导
//                                    ;
//                                else if (activateConstant.equals(ActivateConstant.HEADREST))// 头垫
//                                    MySharedPreferencesUtil.setActivateHeadPad(context, true);
//                                else if (activateConstant.equals(ActivateConstant.PILLOW))// 枕头
//                                    MySharedPreferencesUtil.setActivatePillow(context, true);
//                                else if (activateConstant.equals(ActivateConstant.MATTRESS))// 床垫
//                                    MySharedPreferencesUtil.setActivateMattess(context, true);
//                                else if (activateConstant.equals(ActivateConstant.HEADBAND))// 头带
//                                    MySharedPreferencesUtil.setActivateHeadBand(context, true);
//
//                                if (!MySharedPreferencesUtil.isActivateSnoreMonitor(context)) {
//                                    GetMyActivate2(context, handler, 0);
//                                } else {
//
//                                    sendMsg(ReErrorCode.ERRCODE_SUCEED, handler, msg);
//                                }
//                            } else {
//                                sendMsg(err_code, handler, msg);
//                                // sendMsg(ReErrorCode.ERRCODE_SUCEED, handler,
//                                // msg);
//                            }
//                        } catch (Exception e) {
//                            // TODO: handle exception
//                            sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
//                            e.printStackTrace();
//                        }
//                    } else {
//                        sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
//                    }
//                }
//            });
//        } catch (Exception e) {
//            // TODO: handle exception
//            sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
//            e.printStackTrace();
//        }
//    }

//    /**
//     * 用于激活设置后，重新查询
//     *
//     * @param context
//     * @param handler
//     * @param type
//     */
//    public static void GetMyActivate2(final Context context, final Handler handler, final int type) {
//        HashMap<String, Object> param = new HashMap<String, Object>();
//
//        param.put("app_id", AppConstant.APPID);
//        param.put("member_id", MySharedPreferencesUtil.getMemberId(context));
//        param.put("client_key", MySharedPreferencesUtil.getCilenKey(context));
//
//        JSONObject object = new JSONObject(param);
//        String infoJson = object.toString();
//        HashMap<String, String> PARAMS = new HashMap<String, String>();
//        PARAMS.put("dataJson", infoJson);
//
//        String resultStr = WebServiceUtils.callDotNetWS("GetMyActivate", PARAMS);
//        Log.v("GetMyActivate", "resultStr::" + resultStr);
//        if (resultStr != null) {
//            try {
//                JSONObject temp = new JSONObject(resultStr);
//                int err_code = temp.getInt("err_code");
//                String msg = temp.getString("msg");
//                if (err_code == 0) {
//                    JSONObject data = temp.getJSONObject("data");
//                    MySharedPreferencesUtil.setActivatePillow(context, data.optBoolean("is_activate_pillow", false));
//                    MySharedPreferencesUtil.setActivateHeadPad(context, data.optBoolean("is_activate_headrest", false));
//                    MySharedPreferencesUtil.setActivateSnoreMonitor(context, data.optBoolean("is_activate_snoring", false));
//                    MySharedPreferencesUtil.setActivateMattess(context, data.optBoolean("is_activate_mattress", false));
//                    MySharedPreferencesUtil.setActivateHeadBand(context, data.optBoolean("is_activate_headband", false));
//
//                    sendMsg(ReErrorCode.ERRCODE_SUCEED, handler, msg);
//                } else {
//                    sendMsg(err_code, handler, msg);
//                }
//            } catch (Exception e) {
//                // TODO: handle exception
//                sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
//                e.printStackTrace();
//            }
//        } else {
//            sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
//        }
//    }

    /**
     * 金币激活骨传导音频的接口
     *
     * @param context
     * @param handler
     */
    public static void GoldActivateForBoneConduction(final Context context, final Handler handler) {
        try {
            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    HashMap<String, Object> PARAMS = new HashMap<String, Object>();
                    PARAMS.put("memberId", MySharedPreferencesUtil.getMemberId(context));
                    PARAMS.put("clientKey", MySharedPreferencesUtil.getCilenKey(context));
                    String resultStr = WebServiceUtils.callDotNetWS("GoldActivateForBoneConduction", PARAMS);
                    Log.v("GoldActivateForBoneCon", "resultStr::" + resultStr);
                    if (resultStr != null) {
                        try {
                            JSONObject temp = new JSONObject(resultStr);
                            int err_code = temp.getInt("err_code");
                            String msg = temp.getString("msg");
                            if (err_code == 0) {
                                sendMsg(ReErrorCode.ERRCODE_SUCEED, handler, msg);
                            } else {
                                sendMsg(err_code, handler, msg);
                                // sendMsg(ReErrorCode.ERRCODE_SUCEED, handler,
                                // msg);
                            }
                        } catch (Exception e) {
                            // TODO: handle exception
                            sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
                            e.printStackTrace();
                        }
                    } else {
                        sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
                    }
                }
            });
        } catch (Exception e) {
            // TODO: handle exception
            sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
            e.printStackTrace();
        }
    }

//    /**
//     * 获取激活信息
//     *
//     * @param context
//     * @param handler
//     */
//    public static void GetMyActivate(final Context context, final Handler handler) {
//        try {
//            MyThread.startNewThread(new Runnable() {
//                @Override
//                public void run() {
//                    HashMap<String, Object> param = new HashMap<String, Object>();
//
//                    param.put("app_id", AppConstant.APPID);
//                    param.put("member_id", MySharedPreferencesUtil.getMemberId(context));
//                    param.put("client_key", MySharedPreferencesUtil.getCilenKey(context));
//
//                    JSONObject object = new JSONObject(param);
//                    String infoJson = object.toString();
//                    HashMap<String, String> PARAMS = new HashMap<String, String>();
//                    PARAMS.put("dataJson", infoJson);
//
//                    String resultStr = WebServiceUtils.callDotNetWS("GetMyActivate", PARAMS);
//                    Log.v("GetMyActivate", "resultStr::" + resultStr);
//                    if (resultStr != null) {
//                        try {
//                            JSONObject temp = new JSONObject(resultStr);
//                            int err_code = temp.getInt("err_code");
//                            String msg = temp.getString("msg");
//                            if (err_code == 0) {
//                                JSONObject data = temp.getJSONObject("data");
//                                // MySharedPreferencesUtil.setActivateUploadAudio(context,
//                                // data.optBoolean("is_activate_upload_audio",
//                                // false));
//                                // MySharedPreferencesUtil.setActivateBoneConduction(context,
//                                // data.optBoolean("is_activate_bone_conduction",
//                                // false));
//                                MySharedPreferencesUtil.setActivatePillow(context, data.optBoolean("is_activate_pillow", false));
//                                MySharedPreferencesUtil.setActivateHeadPad(context, data.optBoolean("is_activate_headrest", false));
//                                MySharedPreferencesUtil.setActivateSnoreMonitor(context, data.optBoolean("is_activate_snoring", false));
//                                MySharedPreferencesUtil.setActivateMattess(context, data.optBoolean("is_activate_mattress", false));
//                                MySharedPreferencesUtil.setActivateHeadBand(context, data.optBoolean("is_activate_headband", false));
//                                sendMsg(ReErrorCode.ERRCODE_SUCEED, handler, msg);
//                            } else {
//                                sendMsg(err_code, handler, msg);
//                            }
//                        } catch (Exception e) {
//                            // TODO: handle exception
//                            sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
//                            e.printStackTrace();
//                        }
//                    } else {
//                        sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
//                    }
//                }
//            });
//        } catch (Exception e) {
//            // TODO: handle exception
//            sendMsg(ReErrorCode.ERRCODE_NO_RETRUN_ERROR, handler, null);// ERRCODE_NO_RETRUN_ERROR
//            e.printStackTrace();
//        }
//    }

    private static void sendMsg(int flag, Handler handler, Object msg) {
        Message message = new Message();
        message.what = flag;
        message.obj = msg;
        handler.sendMessage(message);
    }

}
